#!/usr/bin/python
#
# Generate makefiles for python unittests and django tests
#
import glob
import os

import settings

class Generator(object):
    def __init__(self, type):
        assert type in ('django', 'python',)
        self.type = type

        if self.is_python():
            print 'PYTHON_TESTS += \\'

        if self.is_django():
            print 'DJANGO_TESTS += \\'

    def is_django(self):
        return 'django' == self.type

    def is_python(self):
        return 'python' == self.type

    def generate_app(self, app, is_last=False):
        if self.is_python():
            files = list(glob.glob('%s/tests/ut_*.py' % app))
            count = len(files)
            for i, filename in enumerate(files):
                last = is_last and (count - 1) == i
                end = '' if last else ' \\'
                print '\t%s.pythontest%s' % (filename.split('.')[0], end)

        elif self.is_django():
            end = '' if is_last else ' \\'
            print '\t%s.djangotest%s' % (app, end)

    def generate(self):
        count = len(settings.MY_APPS)
        for i, app in enumerate(settings.MY_APPS):
            self.generate_app(app, is_last=(count - 1) == i)

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('type', help='Type of makefile to generate')
    args = parser.parse_args()

    Generator(args.type).generate()
